package com.cmbchina.entity;

import java.math.BigDecimal;
import java.util.Objects;

public class ExchangeRate {
    private BigDecimal rate;
    private Currency from;
    private Currency to;

    public ExchangeRate(BigDecimal rate, Currency from, Currency to) {
        this.rate = rate;
        this.from = from;
        this.to = to;
    }

    public Money exchange(Money fromMoney){
        if(Objects.isNull(fromMoney)){
            throw new IllegalArgumentException("money cannot be null");
        }

        if(fromMoney.getCurrency().equals(to.getCurrency())){
            return fromMoney;
        }

        BigDecimal targetAmount = fromMoney.getAmount().multiply(rate);
        return new Money(targetAmount, to);
    }
}
